package de.consileon.resisweb.client.mvp;

import org.gwtopenmaps.openlayers.client.LonLat;
import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.MapOptions;
import org.gwtopenmaps.openlayers.client.MapWidget;
import org.gwtopenmaps.openlayers.client.Projection;
import org.gwtopenmaps.openlayers.client.control.LayerSwitcher;
import org.gwtopenmaps.openlayers.client.control.MousePosition;
import org.gwtopenmaps.openlayers.client.control.NavToolbar;
import org.gwtopenmaps.openlayers.client.control.PanZoomBar;
import org.gwtopenmaps.openlayers.client.layer.Layer;
import org.gwtopenmaps.openlayers.client.layer.OSM;
import org.gwtopenmaps.openlayers.client.layer.WMS;
import org.gwtopenmaps.openlayers.client.layer.WMSOptions;
import org.gwtopenmaps.openlayers.client.layer.WMSParams;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.consileon.resisweb.client.ClientFactory;
import de.consileon.resisweb.client.panel.suchen.SuchenPlace;

public class MainFrame extends Composite implements  AcceptsOneWidget, EntryPoint {

	
	@UiField 
	Button actionBtn;
	
	@UiField
	MapWidget mapWidget;
	
	
	@UiField OneWidgetLayoutPanel toolPanel;
	private Place defaultPlace = new SuchenPlace("World!");
	
	
	
	private static MainFrameUiBinder uiBinder = GWT
			.create(MainFrameUiBinder.class);

	interface MainFrameUiBinder extends UiBinder<SplitLayoutPanel, MainFrame> {
	}
	
	 private MapOptions defaultMapOptions;
		//private MapWidget mapWidget;
		private Map map;
		
		
		public static final String WMS_URL = "http://labs.metacarta.com/wms/vmap0";
		private WMS wmsLayer;
	    

	public MainFrame() {
		initWidget(uiBinder.createAndBindUi(this));
		

	}

	

	public OneWidgetLayoutPanel getToolPanel() {
		return toolPanel;
	}

	

	
	
	@Override
	public void setWidget(IsWidget w) {
		toolPanel.clear();
		toolPanel.add(w);
	}



	@Override
	public void onModuleLoad() {
		// Create app layout
        SplitLayoutPanel outer = uiBinder.createAndBindUi(this);
		// Create ClientFactory using deferred binding so we can replace with different
		// impls in gwt.xml
		ClientFactory clientFactory = GWT.create(ClientFactory.class);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();

		// Start ActivityManager for the main widget with our ActivityMapper
		ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
		ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
//		appWidget.add(new Button("HALLO"));
		activityManager.setDisplay(toolPanel);

		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper= GWT.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);

		RootPanel.get().add(outer);
		// Goes to place represented on URL or default place
		historyHandler.handleCurrentHistory();
//		mapWidget=makeMapWidget();
//		mapPanel.add(mapWidget);
		LonLat lt=map.getCenter();
		System.out.println("Long: "+lt.lon()+"/ Lat: "+lt.lat());
	}


	/** Used by MyUiBinder to instantiate mapWidget */
	@UiFactory 
	  MapWidget makeMapWidget() {
		  
		  this.defaultMapOptions = new MapOptions();
			this.defaultMapOptions.setNumZoomLevels(16);
			String epsg4326="EPSG:4326";
			this.defaultMapOptions.setProjection(epsg4326);
			
		  MapWidget mapWidget=new MapWidget("800px","800px", defaultMapOptions);
		  
		  this.map=mapWidget.getMap();		  
		  OSM osm=OSM.Osmarender("OpenStreetMap");
		  mapWidget.getMap().addLayer(osm);
		  LonLat center=new LonLat(774670,6601917);
//		  center.transform(epsg4326,map.getProjection());
		  map.setCenter(center,12);
//		  map.setCenter(new LonLat(100,100));
		  

	    return mapWidget;
	  }
	
	  
	  public static native void addBingMaps(Map map)/*-{
//            map.addControl( new $wnd.OpenLayers.Control.LayerSwitcher()); 
            var shaded = new $wnd.OpenLayers.Layer.VirtualEarth("Shaded", {
                type: VEMapStyle.Shaded
            });
            var hybrid = new $wnd.OpenLayers.Layer.VirtualEarth("Hybrid", {
                type: VEMapStyle.Hybrid
            });
            var aerial = new $wnd.OpenLayers.Layer.VirtualEarth("Aerial", {
                type: VEMapStyle.Aerial
            });
 
            map.addLayers([shaded, hybrid, aerial]);
 
            map.setCenter(new OpenLayers.LonLat(-110, 45), 3);
	}-*/;
	
	  
	  @UiHandler("actionBtn")
		void onClickGoodbye(ClickEvent e)
		{
		  System.out.println("Long: "+map.getCenter().lon()+" Lat: "+map.getCenter().lat());
//		  initMapWidget(mapWidget,defaultMapOptions);
//		  LonLat lonLat=new LonLat(10,10);
//		  map.setCenter(lonLat,10);
//		  addBingMaps(map);
		}
}
